package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

//用户模块
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //发送短信
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String,String> param){
        //接受手机号
        String phone = param.get("phone");
        //调用service发送短信
        userService.sendMsg(phone);
        //返回结果
        return ResultInfo.success();
    }

    //用户注册登录
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String,String> map){
        //接受参数
        String phone = map.get("phone");//接受传入的手机号信息
        String code = map.get("code");//接受传入的验证码信息
        //调用service层方法，并返回数据结果
        return userService.login(phone,code);
    }

    //用户退出登录
    @PostMapping("/user/logout")
    public ResultInfo logout(HttpSession session) {
        // 1.清理session
        session.invalidate();
        // 2.返回结果
        return ResultInfo.success();
    }

}
